package com.clothing.customization.service.impl;

import com.clothing.customization.dto.AccessoryInventoryDTO;
import com.clothing.customization.dto.AccessoryInventoryQueryDTO;
import com.clothing.customization.entity.AccessoryInventory;
import com.clothing.customization.repository.AccessoryInventoryRepository;
import com.clothing.customization.service.AccessoryInventoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AccessoryInventoryServiceImpl implements AccessoryInventoryService {

    private final AccessoryInventoryRepository accessoryInventoryRepository;

    @Override
    public Page<AccessoryInventoryDTO> getAccessoryList(AccessoryInventoryQueryDTO queryDTO) {
        Specification<AccessoryInventory> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            
            if (StringUtils.hasText(queryDTO.getAccessoryCode())) {
                predicates.add(cb.like(root.get("accessoryCode"), "%" + queryDTO.getAccessoryCode() + "%"));
            }
            if (StringUtils.hasText(queryDTO.getAccessoryName())) {
                predicates.add(cb.like(root.get("accessoryName"), "%" + queryDTO.getAccessoryName() + "%"));
            }
            if (StringUtils.hasText(queryDTO.getAccessoryType())) {
                predicates.add(cb.equal(root.get("accessoryType"), queryDTO.getAccessoryType()));
            }
            if (StringUtils.hasText(queryDTO.getColor())) {
                predicates.add(cb.equal(root.get("color"), queryDTO.getColor()));
            }
            if (StringUtils.hasText(queryDTO.getSupplier())) {
                predicates.add(cb.like(root.get("supplier"), "%" + queryDTO.getSupplier() + "%"));
            }
            
            return cb.and(predicates.toArray(new Predicate[0]));
        };

        PageRequest pageRequest = PageRequest.of(queryDTO.getPageNum() - 1, queryDTO.getPageSize(), 
            Sort.by(Sort.Direction.DESC, "createTime"));

        return accessoryInventoryRepository.findAll(spec, pageRequest).map(this::convertToDTO);
    }

    @Override
    public AccessoryInventoryDTO getAccessoryDetail(Long id) {
        return accessoryInventoryRepository.findById(id)
                .map(this::convertToDTO)
                .orElseThrow(() -> new RuntimeException("配饰不存在"));
    }

    @Override
    @Transactional
    public AccessoryInventoryDTO createAccessory(AccessoryInventoryDTO accessoryDTO) {
        if (accessoryInventoryRepository.existsByAccessoryCode(accessoryDTO.getAccessoryCode())) {
            throw new RuntimeException("配饰编号已存在");
        }

        AccessoryInventory accessory = new AccessoryInventory();
        BeanUtils.copyProperties(accessoryDTO, accessory);
        
        return convertToDTO(accessoryInventoryRepository.save(accessory));
    }

    @Override
    @Transactional
    public AccessoryInventoryDTO updateAccessory(Long id, AccessoryInventoryDTO accessoryDTO) {
        if (accessoryInventoryRepository.existsByAccessoryCodeAndIdNot(accessoryDTO.getAccessoryCode(), id)) {
            throw new RuntimeException("配饰编号已存在");
        }

        AccessoryInventory accessory = accessoryInventoryRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("配饰不存在"));
        
        BeanUtils.copyProperties(accessoryDTO, accessory, "id", "createTime");
        
        return convertToDTO(accessoryInventoryRepository.save(accessory));
    }

    @Override
    @Transactional
    public void deleteAccessory(Long id) {
        if (!accessoryInventoryRepository.existsById(id)) {
            throw new RuntimeException("配饰不存在");
        }
        accessoryInventoryRepository.deleteById(id);
    }

    private AccessoryInventoryDTO convertToDTO(AccessoryInventory accessory) {
        AccessoryInventoryDTO dto = new AccessoryInventoryDTO();
        BeanUtils.copyProperties(accessory, dto);
        return dto;
    }
} 